package com.laoyang.rabbit.config;

import org.springframework.amqp.core.AcknowledgeMode;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.config.SimpleRabbitListenerContainerFactory;
import org.springframework.amqp.rabbit.connection.CachingConnectionFactory;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
import org.springframework.boot.autoconfigure.amqp.SimpleRabbitListenerContainerFactoryConfigurer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.annotation.Resource;
import java.util.StringJoiner;

/**
 * @author com.yyy
 * @Date 2020-09-11 21:21
 * @Email yangyouyuhd@163.com
 */
@Configuration
public class RabbitConfig {

    @Resource
    private CachingConnectionFactory connectionFactory;

    @Resource
    private SimpleRabbitListenerContainerFactoryConfigurer factoryConfigurer;

    /**
     * 单一消费者实例的配置
     * @return
     */
    @Bean
    public SimpleRabbitListenerContainerFactory simpleRabbitListenerContainerFactory(){

        SimpleRabbitListenerContainerFactory factory = new SimpleRabbitListenerContainerFactory();
        // 包装容器提供的工厂
        factory.setConnectionFactory(connectionFactory);
        // 消息转化器
        factory.setMessageConverter(new Jackson2JsonMessageConverter());
        // 并发消费大小
        factory.setConcurrentConsumers(1);
        // 最大并发消费大小
        factory.setMaxConcurrentConsumers(1);
        // 每个消费者实例只能拉取一个消息
        factory.setPrefetchCount(1);
        return factory;
    }


    // @Bean
    public SimpleRabbitListenerContainerFactory multiRabbitListenerContainerFactory(){

        SimpleRabbitListenerContainerFactory factory = new SimpleRabbitListenerContainerFactory();
        // none 不需要确认消费
        factory.setAcknowledgeMode(AcknowledgeMode.NONE);
        // 包装容器提供的工厂
        factory.setConnectionFactory(connectionFactory);
        // 消息转化器
        factory.setMessageConverter(new Jackson2JsonMessageConverter());
        // 并发消费大小
        factory.setConcurrentConsumers(10);
        // 最大并发消费大小
        factory.setMaxConcurrentConsumers(15);
        // 每个消费者实例只能拉取一个消息
        factory.setPrefetchCount(10);
        return factory;
    }


    @Bean
    public RabbitTemplate rabbitTemplate(){
        // 开启到交换机确认
        connectionFactory.setPublisherConfirms(true);
        // 开启确认后后调、可以打日志
        connectionFactory.setPublisherReturns(true);

        RabbitTemplate template = new RabbitTemplate(connectionFactory);
        template.setMandatory(true);
        // 到交换机确认
        template.setConfirmCallback(new RabbitTemplate.ConfirmCallback() {
            @Override
            public void confirm(CorrelationData correlationData, boolean ack, String cause) {
                System.out.println("消息发送成功");
            }
        });
        // 确认后后调、可以打日志、或者消息丢失
        template.setReturnCallback(new RabbitTemplate.ReturnCallback() {
            @Override
            public void returnedMessage(Message message, int i, String s, String s1, String s2) {
                System.out.println("消息丢失");
            }
        });
        return template;
    }




}
